package com.example.adms.common.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.example.adms.user.service.UserService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;


/**
 * 读监听器
 */
@Slf4j
public class UserExcelDataListener extends AnalysisEventListener<UserExcelData> {

    private List<UserExcelData> list = new ArrayList<>();
    private UserService userService;

    public UserExcelDataListener(UserService userService){
        this.userService = userService;
    }
    /**
     * 这个每一条数据解析都会来调用
     * @param data
     * @param context
     */
    @Override
    public void invoke(UserExcelData data, AnalysisContext context) {
        log.info("解析到一条数据：{}",JSONObject.toJSONString(data));
        list.add(data);
    }

    /**
     * 所有数据解析完成了 都会来调用
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("读取完毕,数据:{}",list);
        //读取完成后将数据交给service做处理
        userService.addUserByExcel(list);
    }

}
